package com.grandtech.insurance.common.utils.collect;

import com.grandtech.insurance.common.utils.trans.ListUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 仿照mongo的
 */
public class MultiCriteria implements Serializable {

    private List<Criteria> criteriaList = new ArrayList<>();

    public MultiCriteria() {

    }

    public static MultiCriteria create(List<Criteria> criteriaList) {
        if (criteriaList == null) return null;
        return new MultiCriteria(criteriaList);
    }

    /**
     * 构造函数1
     *
     * @param fieldName
     */
    private MultiCriteria(String fieldName) {
        this.criteriaList.add(new Criteria(fieldName));
    }

    /**
     * 构造函数2
     *
     * @param fieldName
     * @param criteriaList
     */
    private MultiCriteria(String fieldName, List<Criteria> criteriaList) {
        this.criteriaList.addAll(criteriaList);
        this.criteriaList.add(new Criteria(fieldName));
    }

    private MultiCriteria(List<Criteria> criteriaList) {
        this.criteriaList = criteriaList;
    }

    public static MultiCriteria where(String fieldName) {
        return new MultiCriteria(fieldName);
    }


    public MultiCriteria add(String fieldName) {
        return new MultiCriteria(fieldName, criteriaList);
    }

    public MultiCriteria is(Object fieldValue) {
        Criteria criteria = ListUtil.getLastItemT(criteriaList);
        criteria.setLinkName(" = ");
        criteria.setFieldValue(fieldValue);
        return this;
    }

    public MultiCriteria like(Object fieldValue) {
        Criteria criteria = ListUtil.getLastItemT(criteriaList);
        criteria.setLinkName(" like ");
        criteria.setFieldValue(fieldValue);
        return this;
    }

    public MultiCriteria not(Object fieldValue) {
        Criteria criteria = ListUtil.getLastItemT(criteriaList);
        criteria.setLinkName("<>");
        criteria.setFieldValue(fieldValue);
        return this;
    }

    public MultiCriteria order(String fieldName) {
        return new MultiCriteria(fieldName, criteriaList);
    }

    public MultiCriteria order(Object fieldValue) {
        Criteria criteria = ListUtil.getLastItemT(criteriaList);
        criteria.setLinkName("order by");
        criteria.setFieldValue(fieldValue);
        return this;
    }

    public MultiCriteria getMultiCriteria() {
        return this;
    }

    public List<Criteria> getListCriteria() {
        return criteriaList;
    }

    public boolean hasQuery() {
        boolean flag = false;
        Criteria criteria;
        for (int i = 0; i < criteriaList.size(); i++) {
            criteria = criteriaList.get(i);
            if (criteria.getLinkName().equals("=") || criteria.getLinkName().equals("like") || criteria.getLinkName().equals("<>"))
                return true;
        }
        return flag;
    }

    public boolean hasOrder() {
        boolean flag = false;
        Criteria criteria;
        for (int i = 0; i < criteriaList.size(); i++) {
            criteria = criteriaList.get(i);
            if (criteria.getLinkName().equals("order by")) return true;
        }
        return flag;
    }
}
